<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit" width="60%">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { defineProps, ref, computed, unref, reactive } from 'vue';
  import { BasicModal, useModalInner } from '/src/components/Modal';
  import { BasicForm, useForm } from '/src/components/Form';
  import { formSchemaQJ } from '../myApply.data';
  import { addApply } from '../myApply.api';
  // 声明Emits
  const emit = defineEmits(['success', 'register']);
  const props = defineProps({ dictId: String });
  const isUpdate = ref(true);
  const tempObj=ref();
  const lcId=ref();
  //表单配置
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    schemas: formSchemaQJ,
    showActionButtonGroup: false,
    mergeDynamicData: props,
    labelCol: {
      xs: { span: 24 },
      sm: { span: 4 },
    },
    wrapperCol: {
      xs: { span: 24 },
      sm: { span: 18 },
    },
  });
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    lcId.value=data.record.id;
    tempObj.value=data.record;
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;
    if (unref(isUpdate)) {
      //表单赋值
      await setFieldsValue({
        ...data.record,
      });
    }
  });

  //设置标题
  const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));

  //表单提交事件
  async function handleSubmit(v) {
    try {
      setModalProps({ confirmLoading: true });
      const values = await validate();
      let formData = Object.assign(tempObj.value, values)
      formData.procDefId = lcId.value;
      formData.procDeTitle = tempObj.value.name;
      formData.tableName = tempObj.value.businessTable;
      formData.filedNames="name,descc,age,chegnshi";
      formData.name=formData.qjName;
      console.log("formData",formData)
      //  if(getTitle.value=='新增'){
      //   formData.id=null;
      //  }
      /**
       * {
        "name": "12",
        "descc": "12",
        "age": "12",
        "chegnshi": "21",
        "procDefId": "请假审批:1:5245",
        "procDeTitle": "请假审批",
        "tableName": "test_demo",
        "filedNames": "name,descc,age,chegnshi"
          }
       */
      //提交表单
      await addApply(formData);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
